package jpa;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class Controller
 */
@WebServlet("/Controller")
public class Controller extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private Map<String, IRequestHandler> m_map;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Controller() 
    {
        super();
        m_map = new HashMap<String, IRequestHandler>();
        m_map.put("DUMP", new DumpHandler());
        m_map.put("VIEWCACHE", new ViewHandler());
        m_map.put("CREATECACHEPAGE", new CreateCachePageHandler());
        m_map.put("CACHE", new CacheHandler());
        m_map.put("INDEX", new IndexHandler());
        m_map.put("TOUR", new TourHandler());
        m_map.put("SEARCH", new SearchHandler());
        m_map.put("PROFILE", new ProfileHandler());
        m_map.put("ADMIN", new AdminHandler());
        m_map.put("LOGIN", new LoginHandler());
        m_map.put("RANDOM", new RandomHandler());
        m_map.put("APPROVECACHE", new ApproveHandler());
        m_map.put("DELETECACHE", new DeleteCacheHandler());
        m_map.put("DELETEMEMBER", new DeleteMemberHandler());
        m_map.put("CREATECACHE", new CreateCacheHandler());
        m_map.put("LOGINUSER", new LoginUserHandler());
        m_map.put("LOGOFF", new LogoffUserHandler());
        m_map.put("REGISTER", new RegisterUserHandler());
        m_map.put("REGISTERUSER", new RegisterHandler());
        m_map.put("EDITCACHE", new EditHandler());
        m_map.put("CREATEHEADER", new CreateHeaderHandler());
        m_map.put("EDITHEADER", new EditHeaderHandler());
        
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException 
	{
		super.init(config);
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		String page = request.getParameter("do");
        System.out.println("Controller page request: " + page);

        Context context= null;
        IStatelessBean stateless = null;
        IStatefulBean stateful = null;
        try
        {
        	
            context = new InitialContext();
            stateless = (IStatelessBean) context.lookup("java:comp/env/StatelessBean"); 
            HttpSession session = request.getSession();
            session.setAttribute("sessionBean", stateless);
            
            if (session.getAttribute("statefulBean") == null)
            {
                session.setAttribute("statefulBean", (IStatefulBean) context.lookup("java:comp/env/StatefulBean"));    
            }
            stateful = (IStatefulBean)session.getAttribute("statefulBean"); 
        }
        catch (NamingException e)
        {
            throw new ServletException(e);
        }

        if (page == null)
        {
            page = "index";
        }

        IRequestHandler handler = null;
        handler = m_map.get(page.toUpperCase());
        if (handler == null)
        {
            throw new ServletException("Unknown request: " + page);
        }
        handler.handleRequest(request, response, stateless, stateful, getServletContext());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	/**
	 * @see HttpServlet#doPut(HttpServletRequest, HttpServletResponse)
	 */
	protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{	
		
		
		
	}
	

}
